package dataAccess;

import java.sql.*;
import java.util.*;

import dataAccess.Connector;
import dataAccess.DALException;
import dataAccess.PriceDTO;

public class MySQLPriceDAO implements PriceDAO{

	public PriceDTO getPrice(int priceID) throws DALException {
		ResultSet rs = Connector
				.doQuery("SELECT * FROM Price WHERE Price_ID = " + priceID);
		try {
			if (!rs.first())
				throw new DALException("Prisen " + priceID + " findes ikke");
			return new PriceDTO(rs.getInt("Price_ID"), rs.getInt("Field_ID"),
					rs.getInt("FieldPrice"), rs.getInt("HousePrice"));
		} catch (SQLException e) {
			throw new DALException(e);
		}
	}

	public List<PriceDTO> getPriceList() throws DALException {
		List<PriceDTO> data = new ArrayList<PriceDTO>();
		ResultSet rs = Connector.doQuery("SELECT * FROM Price");
		try {
			while (rs.next()) {
				data.add(new PriceDTO(rs.getInt("Price_ID"), rs
						.getInt("Field_ID"), rs.getInt("FieldPrice"), rs
						.getInt("HousePrice")));
			}
		} catch (SQLException e) {
			throw new DALException(e);
		}
		return data;
	}

	public void createPrice(PriceDTO price) throws DALException {
		Connector.doUpdate("INSERT INTO Price VALUES (" + price.getPriceID()
				+ ", " + price.getFieldID() + ", " + price.getFieldPrice()
				+ ", " + price.getHousePrice() + ");");
	}

	public void updatePrice(PriceDTO price) throws DALException {
		Connector.doUpdate("UPDATE Price SET Field_ID = " + price.getFieldID()
				+ ", FieldPrice = " + price.getFieldPrice() + ", HousePrice = "
				+ price.getHousePrice() + " WHERE Price_ID = "
				+ price.getPriceID() + ";");
	}

}
